#include "stdafx.h"
#include "CommonRaknetApi.h"

MessageID RakNet::GetPacketIdentifier( Packet *p )
{
	if ((unsigned char)p->data[0] == ID_TIMESTAMP)
		return (MessageID) p->data[sizeof(unsigned char) + sizeof(unsigned long)];
	
	return (MessageID) p->data[0];
}

void RakNet::WriteStringToBitStream( BitStream& bitStream, CString& str )
{
	WORD chatLen = (WORD)str.GetLength()*sizeof(TCHAR);

	bitStream.Write(chatLen);
	bitStream.WriteAlignedBytes((const BYTE*)str.GetBuffer(), chatLen);
}

void RakNet::ReadStringFromBitStream( BitStream& bitStream, CString& str )
{
	WORD len = 0;
	bitStream.Read(len);
	BYTE* pBuffer = new BYTE[len+sizeof(TCHAR)];
	bitStream.ReadAlignedBytes(pBuffer, len);
	// set Null value
	memset(&pBuffer[len], 0, sizeof(TCHAR));
	str.Format(_T("%s"), pBuffer);

	delete [] pBuffer;
}